import Cookies from 'js-cookie'

const TokenKey = 'Admin-Token'
const RefreshTokenKey = 'Refresh-Admin-Token'

export function setNav(val, key) {
  return Cookies.set(val, key)
}

export function getNav(val) {
  return Cookies.get(val)
}

export function getToken() {
  if (sessionStorage.getItem(TokenKey)) {
    return Cookies.get(TokenKey)
  } else {
    Cookies.remove(TokenKey)
    return null
  }
}

export function setToken(token) {
  sessionStorage.setItem(TokenKey, TokenKey)
  return Cookies.set(TokenKey, token)
}

export function removeToken() {
  sessionStorage.removeItem(TokenKey)
  return Cookies.remove(TokenKey)
}

export function getRefreshToken() {
  if (sessionStorage.getItem(RefreshTokenKey)) {
    return Cookies.get(RefreshTokenKey)
  } else {
    Cookies.remove(RefreshTokenKey)
    return null
  }
}

export function setRefreshToken(token) {
  sessionStorage.setItem(RefreshTokenKey, RefreshTokenKey)
  return Cookies.set(RefreshTokenKey, token)
}

export function removeRefreshToken() {
  sessionStorage.removeItem(RefreshTokenKey)
  return Cookies.remove(RefreshTokenKey)
}

export function setTokenWithTime(sec) {
  if (Cookies.get(TokenKey)) {
    if (sec) {
      return Cookies.set(TokenKey, Cookies.get(TokenKey), {
        expires: new Date(new Date().getTime() + sec)
      })
    } else {
      return Cookies.set(TokenKey, Cookies.get(TokenKey))
    }
  }
}

export function setRefreshTokenWithTime(sec) {
  if (Cookies.get(RefreshTokenKey)) {
    if (sec) {
      return Cookies.set(RefreshTokenKey, Cookies.get(RefreshTokenKey), {
        expires: new Date(new Date().getTime() + sec)
      })
    } else {
      return Cookies.set(RefreshTokenKey, Cookies.get(RefreshTokenKey))
    }
  }
}
